package com.lei.controller;

import com.alibaba.fastjson.JSON;
import com.lei.entity.Ck;
import com.lei.entity.LayDate;
import com.lei.entity.Ruku;
import com.lei.service.RukuService;
import com.lei.service.impl.RukuServiceImpl;
import com.lei.util.PageBean;
import com.lei.util.TransBean;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author Lei Chu
 * @Date 2020/10/22 17:11
 */
@WebServlet("/rukuServlet/*")
public class RukuServlet extends BaseServlet {

    private final RukuService rukuService = new RukuServiceImpl();

    /**
     * 功能描述:添加入库单，入库详情单，商品数量增加
     *
     * @param request  Req
     * @param response Resp
     * @return java.lang.String
     */
    public String ruku(HttpServletRequest request, HttpServletResponse response) {
        // 将接收到的表单数据转化成 map 用于添加入库单
        Map map = (Map) JSON.parse(request.getParameter("data"));

        // 将接收的入库表格数据 转化成 List<Map> 用于添加入库详情单 和 让图书数量增加
        List<Map> tableData = (List<Map>) JSON.parse(request.getParameter("tableData"));

        String result = rukuService.addRuku(tableData, map);
        return result;
    }

    /**
     * 功能描述:查询所有的入库单
     *
     * @param request  Req
     * @param response Resp
     * @return java.lang.String
     */
    public String getAllRuku(HttpServletRequest request, HttpServletResponse response) {
        Ruku ruku = new Ruku();
        TransBean.populate(ruku, request.getParameterMap());
        PageBean<Ruku> page = rukuService.getAllRuku(ruku);
        return JSON.toJSONString(page);
    }

    /**
     * 功能描述:查询所有的类型和各个类型的图书数量，用于首页的柱形图生成
     *
     * @param request  Req
     * @param response Resp
     * @return java.lang.String
     */
    public String getAllRukuDetailByRukuId(HttpServletRequest request, HttpServletResponse response) {
        String rukuid = request.getParameter("rukuid");
        PageBean page = rukuService.getAllRukuDetailByRukuId(rukuid);
        return JSON.toJSONString(page);
    }

    /**
     * 功能描述:添加入库单，入库详情单，商品数量增加
     *
     * @param request  Req
     * @param response Resp
     * @return java.lang.String
     */
    public String ck(HttpServletRequest request, HttpServletResponse response) {
        // 将接收到的表单数据转化成 map 用于添加出库单
        Map map = (Map) JSON.parse(request.getParameter("data"));

        // 将接收的入库表格数据 转化成 List<Map> 用于添加出库详情单 和 让图书数量减少
        List<Map> tableData = (List<Map>) JSON.parse(request.getParameter("tableData"));

        String result = rukuService.addCk(tableData, map);
        return result;
    }

    /**
     * 功能描述:查询所有的入库单
     *
     * @param request  Req
     * @param response Resp
     * @return java.lang.String
     */
    public String getAllCk(HttpServletRequest request, HttpServletResponse response) {
        Ck ck = new Ck();
        TransBean.populate(ck, request.getParameterMap());
        PageBean<Ck> page = rukuService.getAllCk(ck);
        return JSON.toJSONString(page);
    }

    /**
     * 功能描述:查询所有的类型和各个类型的图书数量，用于首页的柱形图生成
     *
     * @param request  Req
     * @param response Resp
     * @return java.lang.String
     */
    public String getAllCkDetailByCkId(HttpServletRequest request, HttpServletResponse response) {
        String rukuid = request.getParameter("rukuid");
        PageBean page = rukuService.getAllCkDetailByCkId(rukuid);
        return JSON.toJSONString(page);
    }

    /**
     * 功能描述:查询指定年指定月的出入库总金额，用于首页的折线图的生成
     *
     * @param request  Req
     * @param response Resp
     * @return java.lang.String
     */
    public String getCkAndRuKuMoney(HttpServletRequest request, HttpServletResponse response) {
        LayDate layDate = new LayDate();
        TransBean.populate(layDate, request.getParameterMap());
        Map<String, List> map = rukuService.getCkAndRuKuMoney(layDate);
        return JSON.toJSONString(map);
    }

}
